package com.tte.common.jvm;

import com.tte.common.model.po.User;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.List;

/**
 * 测试堆栈溢出（内存溢出测试）
 * VM Args：-Xms10m -Xmx10m
 */
@Component
public class HeapOOM {
    public HeapOOM() {
    }

    public HeapOOM(int i) {
        i = this.i;
    }
    public static boolean isTrue = true;
    public static int i = 0;

    /**
     * 模拟堆内存溢出
     */
    public static void testHeap(){
        List<User> list = new ArrayList<>();
        while (isTrue) {
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            list.add(new User());
        }
    }

    /**
     * 模拟栈内存溢出
      */
    public static void testStack( ){
        i++;
        System.out.println(String.format("第%s次调用。",String.valueOf(i)));
        while (isTrue) {
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            testStack();
        }
    }
}
